---
title: "R Notebook"
output: html_document
---

############################################
Title: 4_04_Table_4.rmd
Purpose: This code runs the baseline aggregate level regressions in Table 5 
Last updated: 19-04-2024

Input:
    - Baseline macro data. Latest version: final_agg_macro_data_monthly.csv 

Output:
    - Table 5 (Panel A and B)
  
##########################################



########################################## 
1. Setup
########################################## 

0. Setup *Set your file path here*
```{r, include=FALSE, results='hide'}

rm(list=ls()) 

require(stargazer); require(lubridate); library(sandwich); library(ggplot2); require(dplyr); require(tidyverse); library(readxl); library(sandwich); library(lmtest); library(psych)
require(quantmod); require(dynlm); require(AER); require(vars); require(forecast); require(stargazer); require(strucchange); require(xts); require(lubridate); require(fGarch); require(zoo); require(timeSeries); library(sandwich); require(car);library(fBasics); library(ggplot2); require(dplyr); require(tidyverse); library(readxl); library(sandwich); library(QuantPsyc); library(tseries); library(latticeExtra); library(corrplot); library(psych) ; library(stringr)

#Assign dplyr verbs  
select <- dplyr::select
rename <- dplyr::rename
mutate <- dplyr::mutate
filter <- dplyr::filter
arrange <- dplyr::arrange
distinct <- dplyr::distinct
group_by <- dplyr::group_by
summarise <- dplyr::summarise
lag <- dplyr::lag
lead <- dplyr::lead

options(scipen=999)
options(digits=4)

#Define directories
global <- ".../2024_RFS_Replication_TOSUBMIT"

```

1.1 Load macro data
```{r, include=FALSE, results='hide'}

#Load
final_agg_macro_data <- read_csv(paste0(global, "/Data/final_agg_macro_data_monthly.csv", ""), 
      col_types = cols(...1 = col_skip(), date = col_date(format = "%Y-%m-%d")))


#Define Pred Bond spread
final_agg_macro_data <- final_agg_macro_data %>% 
  mutate(predicted_bond = gz_spread - ebp) %>% 
  mutate(predicted_bond_fd = predicted_bond - dplyr::lag(predicted_bond, n=1)) %>% 
  mutate(ebp_fd = ebp - dplyr::lag(ebp, n=1))  


#Calculate fd
final_agg_macro_data <-  final_agg_macro_data %>% 
  dplyr::mutate(agg_elp_6_v3_fd = agg_elp_6_v3 - dplyr::lag(agg_elp_6_v3, n=1)) %>% 
  dplyr::mutate(agg_pred_6_v3_fd = agg_pred_6.x - dplyr::lag(agg_pred_6.x, n=1))%>% 
  dplyr::mutate(agg_elp_6_v7_fd = agg_elp_6_v7 - dplyr::lag(agg_elp_6_v7, n=1)) %>% 
  dplyr::mutate(agg_pred_6_v7_fd = agg_pred_6.y - dplyr::lag(agg_pred_6.y, n=1))  
```

1.2 Set sample period to use
```{r, results="hide", include=FALSE}

#Select sample period
macrofin_m_2000_extd = subset(final_agg_macro_data, date > "1999-11-01" & date < "2023-04-01")  

#Exclude GFC
macrofin_m_2000_extd_excrisis = subset(macrofin_m_2000_extd, date < "2007-12-01" | date > "2009-03-01") 


```



########################################## 
2. Table 5 results
##########################################

2.1 OLS Reg: Panel A 
```{r, include=FALSE, results='hide'}

mod_lip = lm(lip_3m_ahead ~  agg_elp_6_v3_fd + agg_pred_6_v3_fd  + termspr + ffr_r  + lip_1m_behind  + first_pc_fd , data = macrofin_m_2000_extd)
summary(mod_lip)
# NewwyWest standard errors
nw_mod_lip = NeweyWest(lm(lip_3m_ahead ~  agg_elp_6_v3_fd + agg_pred_6_v3_fd  + termspr + ffr_r  + lip_1m_behind  + first_pc_fd, data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_lip, vcov = nw_mod_lip )
robust_mod_lip = coeftest(mod_lip, vcov = nw_mod_lip)[,3]
coef_mod_lip=lm.beta(mod_lip)



mod_ism_man = lm(ism_man_3m_ahead ~ agg_elp_6_v3_fd + agg_pred_6_v3_fd  + first_pc_fd +  termspr + ffr_r  + ism_man_1m_behind  , data = macrofin_m_2000_extd)
summary(mod_ism_man)
# NewwyWest standard errors
nw_mod_ism_man = NeweyWest(lm(ism_man_3m_ahead ~ agg_elp_6_v3_fd + agg_pred_6_v3_fd + first_pc_fd +  termspr + ffr_r  + ism_man_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_ism_man, vcov = nw_mod_ism_man )
robust_mod_ism_man = coeftest(mod_ism_man, vcov = nw_mod_ism_man)[,3]
coef_mod_ism_man=lm.beta(mod_ism_man)


mod_ism_nonman = lm(ism_nonman_3m_ahead ~ agg_elp_6_v3_fd + agg_pred_6_v3_fd + first_pc_fd +  termspr + ffr_r  + ism_nonman_1m_behind  , data = macrofin_m_2000_extd)
summary(mod_ism_nonman)
# NewwyWest standard errors
nw_mod_ism_nonman = NeweyWest(lm(ism_nonman_3m_ahead ~ agg_elp_6_v3_fd + agg_pred_6_v3_fd + first_pc_fd +  termspr + ffr_r  + ism_nonman_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_ism_nonman, vcov = nw_mod_ism_nonman )
robust_mod_ism_nonman = coeftest(mod_ism_nonman, vcov = nw_mod_ism_nonman)[,3]
coef_mod_ism_nonman=lm.beta(mod_ism_nonman)


mod_tcu = lm(tcu_3m_ahead ~  agg_elp_6_v3_fd + agg_pred_6_v3_fd  + termspr + ffr_r  + tcu_1m_behind   + first_pc_fd, data = macrofin_m_2000_extd)
summary(mod_tcu)
# NewwyWest standard errors
nw_mod_tcu = NeweyWest(lm(tcu_3m_ahead ~  agg_elp_6_v3_fd + agg_pred_6_v3_fd  + termspr + ffr_r  + tcu_1m_behind  + first_pc_fd, data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_tcu, vcov = nw_mod_tcu )
robust_mod_tcu = coeftest(mod_tcu, vcov = nw_mod_tcu)[,3]
coef_mod_tcu=lm.beta(mod_tcu)


mod_lnew = lm(lnew_3m_ahead ~  agg_elp_6_v3_fd + agg_pred_6_v3_fd  + termspr + ffr_r  + lnew_1m_behind   + first_pc_fd, data = macrofin_m_2000_extd)
summary(mod_lnew)
# NewwyWest standard errors
nw_mod_lnew = NeweyWest(lm(lnew_3m_ahead ~  agg_elp_6_v3_fd + agg_pred_6_v3_fd  + termspr + ffr_r  + lnew_1m_behind + first_pc_fd , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_lnew, vcov = nw_mod_lnew )
robust_mod_lnew = coeftest(mod_lnew, vcov = nw_mod_lnew)[,3]
coef_mod_lnew=lm.beta(mod_lnew)


mod_linv = lm(linv_3m_ahead ~  agg_elp_6_v3_fd + agg_pred_6_v3_fd  + termspr + ffr_r  + linv_1m_behind  + first_pc_fd , data = macrofin_m_2000_extd)
summary(mod_linv)
# NewwyWest standard errors
nw_mod_linv = NeweyWest(lm(linv_3m_ahead ~ agg_elp_6_v3_fd + agg_pred_6_v3_fd  + termspr + ffr_r  + linv_1m_behind  + first_pc_fd, data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_linv, vcov = nw_mod_linv )
robust_mod_linv = coeftest(mod_linv, vcov = nw_mod_linv)[,3]
coef_mod_linv=lm.beta(mod_linv)


stargazer(mod_lip, mod_tcu, mod_lnew, mod_linv, mod_ism_man, mod_ism_nonman, 
          type = "text", 
          align = TRUE, 
          #no.space = TRUE, 
          #dep.var.labels = c("coincident Index - 12m ahead"), 
          se = list(robust_mod_lip,  robust_mod_tcu, robust_mod_lnew, robust_mod_linv, robust_mod_ism_man, robust_mod_ism_nonman), 
          coef = list(coef_mod_lip,  coef_mod_tcu, coef_mod_lnew, coef_mod_linv, coef_mod_ism_man,  coef_mod_ism_nonman ),
          omit = c( "Constant", "lip_1m_behind","ism_man_1m_behind", "ism_nonman_1m_behind", "tcu_1m_behind", "lnew_1m_behind", "linv_1m_behind" )
          )



```

2.2 OLS Reg: Panel B 
```{r, include=FALSE, results='hide'}

mod_lip = lm(lip_3m_ahead ~  agg_elp_6_v7_fd + agg_pred_6_v7_fd  + termspr + ffr_r  + lip_1m_behind  + first_pc_fd , data = macrofin_m_2000_extd)
summary(mod_lip)
# NewwyWest standard errors
nw_mod_lip = NeweyWest(lm(lip_3m_ahead ~  agg_elp_6_v7_fd + agg_pred_6_v7_fd  + termspr + ffr_r  + lip_1m_behind  + first_pc_fd, data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_lip, vcov = nw_mod_lip )
robust_mod_lip = coeftest(mod_lip, vcov = nw_mod_lip)[,3]
coef_mod_lip=lm.beta(mod_lip)



mod_ism_man = lm(ism_man_3m_ahead ~ agg_elp_6_v7_fd + agg_pred_6_v7_fd  + first_pc_fd +  termspr + ffr_r  + ism_man_1m_behind  , data = macrofin_m_2000_extd)
summary(mod_ism_man)
# NewwyWest standard errors
nw_mod_ism_man = NeweyWest(lm(ism_man_3m_ahead ~ agg_elp_6_v7_fd + agg_pred_6_v7_fd + first_pc_fd +  termspr + ffr_r  + ism_man_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_ism_man, vcov = nw_mod_ism_man )
robust_mod_ism_man = coeftest(mod_ism_man, vcov = nw_mod_ism_man)[,3]
coef_mod_ism_man=lm.beta(mod_ism_man)


mod_ism_nonman = lm(ism_nonman_3m_ahead ~ agg_elp_6_v7_fd + agg_pred_6_v7_fd + first_pc_fd +  termspr + ffr_r  + ism_nonman_1m_behind  , data = macrofin_m_2000_extd)
summary(mod_ism_nonman)
# NewwyWest standard errors
nw_mod_ism_nonman = NeweyWest(lm(ism_nonman_3m_ahead ~ agg_elp_6_v7_fd + agg_pred_6_v7_fd + first_pc_fd +  termspr + ffr_r  + ism_nonman_1m_behind , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_ism_nonman, vcov = nw_mod_ism_nonman )
robust_mod_ism_nonman = coeftest(mod_ism_nonman, vcov = nw_mod_ism_nonman)[,3]
coef_mod_ism_nonman=lm.beta(mod_ism_nonman)


mod_tcu = lm(tcu_3m_ahead ~  agg_elp_6_v7_fd + agg_pred_6_v7_fd  + termspr + ffr_r  + tcu_1m_behind   + first_pc_fd, data = macrofin_m_2000_extd)
summary(mod_tcu)
# NewwyWest standard errors
nw_mod_tcu = NeweyWest(lm(tcu_3m_ahead ~  agg_elp_6_v7_fd + agg_pred_6_v7_fd  + termspr + ffr_r  + tcu_1m_behind  + first_pc_fd, data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_tcu, vcov = nw_mod_tcu )
robust_mod_tcu = coeftest(mod_tcu, vcov = nw_mod_tcu)[,3]
coef_mod_tcu=lm.beta(mod_tcu)


mod_lnew = lm(lnew_3m_ahead ~  agg_elp_6_v7_fd + agg_pred_6_v7_fd  + termspr + ffr_r  + lnew_1m_behind   + first_pc_fd, data = macrofin_m_2000_extd)
summary(mod_lnew)
# NewwyWest standard errors
nw_mod_lnew = NeweyWest(lm(lnew_3m_ahead ~  agg_elp_6_v7_fd + agg_pred_6_v7_fd  + termspr + ffr_r  + lnew_1m_behind + first_pc_fd , data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_lnew, vcov = nw_mod_lnew )
robust_mod_lnew = coeftest(mod_lnew, vcov = nw_mod_lnew)[,3]
coef_mod_lnew=lm.beta(mod_lnew)


mod_linv = lm(linv_3m_ahead ~  agg_elp_6_v7_fd + agg_pred_6_v7_fd  + termspr + ffr_r  + linv_1m_behind  + first_pc_fd , data = macrofin_m_2000_extd)
summary(mod_linv)
# NewwyWest standard errors
nw_mod_linv = NeweyWest(lm(linv_3m_ahead ~ agg_elp_6_v7_fd + agg_pred_6_v7_fd  + termspr + ffr_r  + linv_1m_behind  + first_pc_fd, data = macrofin_m_2000_extd), lag = 4, prewhite = F, adjust = T)
coeftest(mod_linv, vcov = nw_mod_linv )
robust_mod_linv = coeftest(mod_linv, vcov = nw_mod_linv)[,3]
coef_mod_linv=lm.beta(mod_linv)


stargazer(mod_lip, mod_tcu, mod_lnew, mod_linv, mod_ism_man, mod_ism_nonman, 
          type = "text", 
          align = TRUE, 
          #no.space = TRUE, 
          #dep.var.labels = c("coincident Index - 12m ahead"), 
          se = list(robust_mod_lip,  robust_mod_tcu, robust_mod_lnew, robust_mod_linv, robust_mod_ism_man, robust_mod_ism_nonman), 
          coef = list(coef_mod_lip,  coef_mod_tcu, coef_mod_lnew, coef_mod_linv, coef_mod_ism_man,  coef_mod_ism_nonman ),
          omit = c( "Constant", "lip_1m_behind","ism_man_1m_behind", "ism_nonman_1m_behind", "tcu_1m_behind", "lnew_1m_behind", "linv_1m_behind" )
          )


```



